﻿namespace JxcLite.Pages.BaseData;

/// <summary>
/// 商业伙伴列表。
/// </summary>
public class PartnerList : BaseTablePage<PartnerInfo>
{
    private IBaseDataService Service;

    /// <summary>
    /// 取得商业伙伴类型（客户、供应商）。
    /// </summary>
    protected virtual string Type { get; }

    protected override async Task OnInitPageAsync()
    {
        await base.OnInitPageAsync();
        Service = await CreateServiceAsync<IBaseDataService>();
        Table.Form = new FormInfo { Width = 1000 };
        Table.OnQuery = QueryPartnersAsync;
    }

    [Action] public void New() => Table.NewForm(Service.SavePartnerAsync, new PartnerInfo { Type = Type });
    [Action] public void DeleteM() => Table.DeleteM(Service.DeletePartnersAsync);
    [Action] public void Edit(PartnerInfo row) => Table.EditForm(Service.SavePartnerAsync, row);
    [Action] public void Delete(PartnerInfo row) => Table.Delete(Service.DeletePartnersAsync, row);
    [Action] public Task Import() => Table.ShowImportAsync();
    [Action] public Task Export() => Table.ExportDataAsync();

    private Task<PagingResult<PartnerInfo>> QueryPartnersAsync(PagingCriteria criteria)
    {
        criteria.SetQuery(nameof(PartnerInfo.Type), QueryType.Equal, Type);
        return Service.QueryPartnersAsync(criteria);
    }
}